//
//  MarkaModelItem.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/21/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit



final class ViewWithImage: UIView {
    
    lazy var iconView: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        return img
    }()
    
    public var isSelected: Bool = false
    
    init () {
        super.init(frame: .zero)
        self.addSubview(iconView)
        iconView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(4)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    internal func addIcon(icon: UIImage?) {
        iconView.image = icon
    }
}

/// Car model UI Table View Cell Element
final class ModelItem: UIView {
    
    // MARK: - UI Elements
    
    private lazy var iconView: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        return img
    }()
    
    private lazy var titleLabel: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        lbl.numberOfLines = 1
        lbl.textColor = .black
        return lbl
    }()
    
    private var titleLblLeading: Constraint?
    private var titleLblTrailing: Constraint?
    
    private lazy var arrowImg: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "arrow")
        return imgView
    }()
    lazy var checkBox: ViewWithImage = {
        let view = ViewWithImage()
        view.layer.cornerRadius = 4.0
        view.layer.borderWidth = 1
        return view
    } ()
    
    // MARK: - Props
    
    private var gesture : GestureWithTag!
    private var isExpandedItem: Bool = true
    
    
    // MARK: - Init
    
    init (isExpandedItem: Bool?=true ) {
        super.init(frame: .zero)
        self.isExpandedItem = isExpandedItem ?? true
        addUIConstraints()
    }

    
    /// add constraints to ui elements
    private func addUIConstraints () {
        self.addSubview(titleLabel)
        self.addSubview(checkBox)
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            titleLblLeading = make.leading.equalToSuperview().inset(16).constraint
            titleLblTrailing = make.trailing.equalToSuperview().inset(16).constraint
        }
        checkBox.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.trailing.equalTo(-16)
            make.centerY.equalToSuperview()
        }
    }
    
    // MARK: - Set Title Text
    
    public func updateText (title: String) {
        titleLabel.text = title
    }
    
    public func addArrow () {
        self.addSubview(arrowImg)
        self.checkBox.removeFromSuperview()
        arrowImg.snp.makeConstraints { (make) in
            make.width.equalTo(8)
            make.height.equalTo(13)
            make.trailing.equalTo(-16)
            make.centerY.equalToSuperview()
        }
    }
    public func addIcon (image: UIImage?) {
        self.addSubview(iconView)
        iconView.image = image
        iconView.layer.cornerRadius = 0
        
        iconView.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.leading.equalTo(16)
            make.centerY.equalToSuperview()
        }
        
        titleLblLeading?.deactivate()
        titleLblTrailing?.deactivate()
        
        titleLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(iconView.snp.trailing).offset(12)
            make.trailing.equalTo(arrowImg.snp.leading).inset(12)
        }
    }
    
    public func fillCircular (_ color: UIColor) {
        iconView.layer.cornerRadius = 10
        iconView.image = nil
        iconView.backgroundColor = color
    }
    
    
    
    public func didSelectCheckbox () {
        checkBox.addIcon(icon: UIImage(named: "correct"))
        checkBox.backgroundColor = UIColor(red: 47/255, green: 130/255, blue: 210/255, alpha: 1)
        checkBox.layer.borderColor = UIColor.clear.cgColor
    }
    
    public func didDeselectCheckbox () {
        self.checkBox.addIcon(icon: nil)
        checkBox.backgroundColor = .clear
        checkBox.layer.borderColor = UIColor.greyBlue.cgColor
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

